﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace SCS.DAL
{
	/// <summary>
	/// 数据访问类:SCS_Operating_Log
	/// </summary>
	public partial class SCS_Operating_Log
	{
		public SCS_Operating_Log()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("Log_ID", "SCS_Operating_Log"); 
		}


		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int Log_ID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from SCS_Operating_Log");
			strSql.Append(" where Log_ID="+Log_ID+" ");
			return DbHelperSQL.Exists(strSql.ToString());
		}

		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(SCS.Model.SCS_Operating_Log model)
		{
			StringBuilder strSql=new StringBuilder();
			StringBuilder strSql1=new StringBuilder();
			StringBuilder strSql2=new StringBuilder();
			if (model.Log_TimeStamp != null)
			{
				strSql1.Append("Log_TimeStamp,");
				strSql2.Append("'"+model.Log_TimeStamp+"',");
			}
			if (model.Log_IP_Adress != null)
			{
				strSql1.Append("Log_IP_Adress,");
				strSql2.Append("'"+model.Log_IP_Adress+"',");
			}
			if (model.Log_MAC_Address != null)
			{
				strSql1.Append("Log_MAC_Address,");
				strSql2.Append("'"+model.Log_MAC_Address+"',");
			}
			if (model.Log_User_ID != null)
			{
				strSql1.Append("Log_User_ID,");
				strSql2.Append("'"+model.Log_User_ID+"',");
			}
			if (model.Log_Description != null)
			{
				strSql1.Append("Log_Description,");
				strSql2.Append("'"+model.Log_Description+"',");
			}
			if (model.Object_Name != null)
			{
				strSql1.Append("Object_Name,");
				strSql2.Append("'"+model.Object_Name+"',");
			}
			strSql.Append("insert into SCS_Operating_Log(");
			strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
			strSql.Append(")");
			strSql.Append(" values (");
			strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
			strSql.Append(")");
			strSql.Append(";select @@IDENTITY");
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(SCS.Model.SCS_Operating_Log model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update SCS_Operating_Log set ");
			if (model.Log_TimeStamp != null)
			{
				strSql.Append("Log_TimeStamp='"+model.Log_TimeStamp+"',");
			}
			else
			{
				strSql.Append("Log_TimeStamp= null ,");
			}
			if (model.Log_IP_Adress != null)
			{
				strSql.Append("Log_IP_Adress='"+model.Log_IP_Adress+"',");
			}
			else
			{
				strSql.Append("Log_IP_Adress= null ,");
			}
			if (model.Log_MAC_Address != null)
			{
				strSql.Append("Log_MAC_Address='"+model.Log_MAC_Address+"',");
			}
			else
			{
				strSql.Append("Log_MAC_Address= null ,");
			}
			if (model.Log_User_ID != null)
			{
				strSql.Append("Log_User_ID='"+model.Log_User_ID+"',");
			}
			else
			{
				strSql.Append("Log_User_ID= null ,");
			}
			if (model.Log_Description != null)
			{
				strSql.Append("Log_Description='"+model.Log_Description+"',");
			}
			else
			{
				strSql.Append("Log_Description= null ,");
			}
			if (model.Object_Name != null)
			{
				strSql.Append("Object_Name='"+model.Object_Name+"',");
			}
			else
			{
				strSql.Append("Object_Name= null ,");
			}
			int n = strSql.ToString().LastIndexOf(",");
			strSql.Remove(n, 1);
			strSql.Append(" where Log_ID="+ model.Log_ID+"");
			int rowsAffected=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rowsAffected > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int Log_ID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from SCS_Operating_Log ");
			strSql.Append(" where Log_ID="+Log_ID+"" );
			int rowsAffected=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rowsAffected > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string Log_IDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from SCS_Operating_Log ");
			strSql.Append(" where Log_ID in ("+Log_IDlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public SCS.Model.SCS_Operating_Log GetModel(int Log_ID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1  ");
			strSql.Append(" Log_ID,Log_TimeStamp,Log_IP_Adress,Log_MAC_Address,Log_User_ID,Log_Description,Object_Name ");
			strSql.Append(" from SCS_Operating_Log ");
			strSql.Append(" where Log_ID="+Log_ID+"" );
			SCS.Model.SCS_Operating_Log model=new SCS.Model.SCS_Operating_Log();
			DataSet ds=DbHelperSQL.Query(strSql.ToString());
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public SCS.Model.SCS_Operating_Log DataRowToModel(DataRow row)
		{
			SCS.Model.SCS_Operating_Log model=new SCS.Model.SCS_Operating_Log();
			if (row != null)
			{
				if(row["Log_ID"]!=null && row["Log_ID"].ToString()!="")
				{
					model.Log_ID=int.Parse(row["Log_ID"].ToString());
				}
				if(row["Log_TimeStamp"]!=null && row["Log_TimeStamp"].ToString()!="")
				{
					model.Log_TimeStamp=DateTime.Parse(row["Log_TimeStamp"].ToString());
				}
				if(row["Log_IP_Adress"]!=null)
				{
					model.Log_IP_Adress=row["Log_IP_Adress"].ToString();
				}
				if(row["Log_MAC_Address"]!=null)
				{
					model.Log_MAC_Address=row["Log_MAC_Address"].ToString();
				}
				if(row["Log_User_ID"]!=null)
				{
					model.Log_User_ID=row["Log_User_ID"].ToString();
				}
				if(row["Log_Description"]!=null)
				{
					model.Log_Description=row["Log_Description"].ToString();
				}
				if(row["Object_Name"]!=null)
				{
					model.Object_Name=row["Object_Name"].ToString();
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select Log_ID,Log_TimeStamp,Log_IP_Adress,Log_MAC_Address,Log_User_ID,Log_Description,Object_Name ");
			strSql.Append(" FROM SCS_Operating_Log ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" Log_ID,Log_TimeStamp,Log_IP_Adress,Log_MAC_Address,Log_User_ID,Log_Description,Object_Name ");
			strSql.Append(" FROM SCS_Operating_Log ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM SCS_Operating_Log ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.Log_ID desc");
			}
			strSql.Append(")AS Row, T.*  from SCS_Operating_Log T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		*/

		#endregion  Method
		#region  MethodEx
        public static string GenerateOperatorLogSQL(SCS.Model.SCS_Operating_Log tOperatorLog)
        {
            string strOperatorLogSQL = string.Empty;

            if (tOperatorLog != null)
            {
                strOperatorLogSQL = "INSERT INTO SCS_Operating_Log(LOG_TIMESTAMP,LOG_IP_ADDRESS,LOG_MAC_ADDRESS,LOG_USER_ID,LOG_DESCRIPTION,OBJECT_NAME) " +
                   "VALUES('" + tOperatorLog.Log_TimeStamp + "','" + tOperatorLog.Log_IP_Adress + "','" + tOperatorLog.Log_MAC_Address +
                   "','" + tOperatorLog.Log_User_ID + "','" + tOperatorLog.Log_Description + "','" + tOperatorLog.Object_Name + "')";
            }
            return strOperatorLogSQL;
        }
		#endregion  MethodEx
	}
}

